package org.jeecg.modules.iot.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.iot.model.Consumption;
import org.jeecg.modules.iot.service.IConsumptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/iot/consumption")
@Api(tags = "能耗服务接口")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @ApiOperation("添加能耗记录")
    @PostMapping("/add")
    public int add(@RequestBody Consumption consumption) {
        return consumptionService.add(consumption);
    }

    @ApiOperation("根据设备ID查询能耗")
    @GetMapping("/queryByDeviceId")
    public Consumption queryByDeviceId(@RequestParam("deviceId") String deviceId) {
        return consumptionService.queryByDeviceId(deviceId);
    }

    @ApiOperation("根据里程查询能耗")
    @GetMapping("/queryByFMileage")
    public Consumption queryByFMileage(@RequestParam("deviceId") String deviceId,
                                       @RequestParam("fMileage") Float fMileage) {
        return consumptionService.queryByFMileage(deviceId, fMileage);
    }

    @ApiOperation("查询时间范围内的能耗记录")
    @GetMapping("/queryConsumptionsByTimeRange")
    public List<Consumption> queryConsumptionsByTimeRange(@RequestParam("deviceId") String deviceId,
                                                          @RequestParam("startTime") Timestamp startTime,
                                                          @RequestParam("endTime") Timestamp endTime) {
        return consumptionService.queryConsumptionsByTimeRange(deviceId, startTime, endTime);
    }

    @ApiOperation("查询最新能耗记录")
    @GetMapping("/queryLatestByDeviceId")
    public Consumption queryLatestByDeviceId(@RequestParam("deviceId") String deviceId) {
        return consumptionService.queryLatestByDeviceId(deviceId);
    }

    @ApiOperation("计算平均能耗")
    @GetMapping("/calculateAverageEConsum")
    public Float calculateAverageEConsum(@RequestParam("deviceId") String deviceId,
                                         @RequestParam("n") int n) {
        return consumptionService.calculateAverageEConsum(deviceId, n);
    }

    @ApiOperation("计算里程加权平均能耗")
    @GetMapping("/calculateMileageWeightedAverageEConsum")
    public Float calculateMileageWeightedAverageEConsum(@RequestParam("deviceId") String deviceId,
                                                        @RequestParam("n") int n) {
        return consumptionService.calculateMileageWeightedAverageEConsum(deviceId, n);
    }
}